package cn.iocoder.yudao.module.system.service.ivrs;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.iocoder.yudao.framework.common.exception.ServiceException;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.List;
import java.util.Map;

/**
 * TODO
 *
 * @author sun
 * @date 16:28 2024/9/21
 **/
@Service
@Validated
@Slf4j
public class IvrsServiceImpl implements IvrsService {

    @Value("${ai.url}")
    private String aiUrl;
    @Value("${ai.path}")
    private String aiPath;

//    private static String aiUrl = "http://10.101.221.237/DL-IVRS";


    public void mattingHuman(String image, String out) {
        String uri = "/matting/human/file";
        HttpRequest request = HttpRequest.post(aiUrl + uri);
        request.form("file", new File(image));
        HttpResponse response = request.execute();
        if (response.isOk()) {
            String result = response.body();
            JSONObject jsonObject = JSON.parseObject(result);
            if (jsonObject.getInteger("code") == 1) {
                String base64 = jsonObject.getJSONObject("result").getString("base64");
                base64TOImage(base64, out);
            }
        }
    }

    public String mattingHumanVideo(String url) {
        String uri = "/matting/human/video/url";
        HttpRequest request = HttpRequest.post(aiUrl + uri);
        request.form("url", url);
        HttpResponse response = request.execute();
        if (response.isOk()) {
            String result = response.body();
            JSONObject jsonObject = JSON.parseObject(result);
            log.info(result);
            if (jsonObject.getInteger("code") == 1 || jsonObject.getInteger("code") == 0) {
                return jsonObject.getString("result").replace("172.17.0.4","10.101.221.237");
            } else {
                throw new ServiceException(501, "视频人脸提取失败!");
            }
        } else {
            throw new ServiceException(501, "视频人脸提取失败!");
        }
    }

    private void base64TOImage(String base64, String out) {
        // 将Base64字符串解码为字节数组
        byte[] imageBytes = Base64.getDecoder().decode(base64);
        try (FileOutputStream fos = new FileOutputStream(out)) {
            fos.write(imageBytes);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String ovrImagePath(String filePath) {
        log.info(aiUrl + "/ocr/image/path");
        HttpRequest request = HttpRequest.post(aiUrl + "/ocr/image/path");
        request.form("path", aiPath + filePath);
        HttpResponse response = request.execute();
        StringBuffer datas = new StringBuffer();
        if (response.isOk()) {
            String result = response.body();
            JSONObject jsonObject = JSON.parseObject(result);
            log.info(result);
            if (jsonObject.getInteger("code") == 1 || jsonObject.getInteger("code") == 0) {
                List<Map<String, Object>> data = jsonObject.getObject("result", List.class);
                for (Map<String, Object> datum : data) {
                    datas.append(datum.get("text") + ";");
                }
            } else {
                throw new ServiceException(501, "OCR处理失败!");
            }
        } else {
            throw new ServiceException(501, "OCR处理失败!");
        }
        return datas.toString();
    }

    public static void main(String[] args) {
       // String a = "http://10.101.221.237/DL-IVRS";
       // String b = "/DATA/production/picture/071bdb248b0b47fb8545b2c964a93a29.png".split("/DATA")[1];
       // String c = "/opt/DL-IVRS-V9-GPU_20240527/DATA";

        //mattingHuman("C:\\Users\\wangsong\\Downloads\\msk.jpeg", "C:\\Users\\wangsong\\Downloads\\msk.png");
        //System.out.println(mattingHumanVideo("/opt/DL-IVRS-V9-GPU_20240527/DATA/reslib/upload/videos/2024/09/10/172595110850625947.mp4"));
    }
}
